<?php
/**
 * 权限管理控制器
 * @author haiqin
 *
 */ 
namespace Users\Controller;
use Admin\Controller\AdminbaseController;
class AuthadminController extends AdminbaseController{
	protected $m;
	
	function _initialize() {
		parent::_initialize();
		$this->m = M('AuthRule');
	}
	
	/**
	 * @name 权限管理
	 * @auth adminleft
	 */
	public function index(){
		if($_GET['action']){
			$_SESSION['cur_auth_action'] = $_GET['action'];
		}
		if(!$_SESSION['cur_auth_action']){
			$_SESSION['cur_auth_action'] = 'add';
		}
		
		if(!$_SESSION['auth_group']){
			$_SESSION['auth_group'] = 'admin';
		}
		if($_GET['group']){
			$_SESSION['auth_group'] = $_REQUEST['group'];
		}
		$this->auth_group = $_SESSION['auth_group']; //输出查询的分组
		
		/*
		$tree = new \Think\Tree();
		$map['group'] = $_SESSION['auth_group'];
		$data = $this->m->where($map)->order('listorder desc')->select();
		

		$tree->init($data);
		$treedata = $tree->get_tree_array(0, $str);
		$this->treedata = $treedata;	
		*/
		$this->display();
	}
	
	public function edit(){
		$tree = new \Think\Tree();
		
		$parentid = intval(I("get.parentid"));
		
		if($_GET['id']){
			$this->info = $this->m->find($_GET['id']);
			$parentid = $this->info['parentid'];
		}
		if(!$_SESSION['auth_group']){
			$_SESSION['auth_group'] = 'admin';
		}
		$map['group'] = $_SESSION['auth_group'];
		$data = $this->m->where($map)->order('listorder desc')->select();
				
		foreach ($data as $r) {
			$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
			$array[] = $r;
		}
		$str = "<option value='\$id' \$selected>\$spacer \$title</option>";
		$tree->init($array);
		$select_categorys = $tree->get_tree(0, $str);
		$this->assign("select_categorys", $select_categorys);
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$_POST['update_time'] = date('Y-m-d H:i:s');
			$this->m->create();
			if($_POST['id']){
				if($this->m->save()){
					$this->_update_file_auths($_POST['group']); //更新到文件缓存
					$this->_update_cache_tree($_POST['group']);
					$this->success('修改成功',U('Authadmin/edit'));
				}else{
					$this->error('修改失败');
				}
			}else{
				if($this->m->add()){
					$this->_update_file_auths($_POST['group']); //更新到文件缓存
					$this->_update_cache_tree($_POST['group']);
					$this->success('添加成功',U('Authadmin/edit'));
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	
	/**
	 * 获取权限
	 * @param string $group
	 * @return Ambigous <mixed, void, boolean, NULL>
	 */
	public function _get_file_auths($group){
		$auths = F('Auths/'.$group);
		if(!$auths){
			AuthadminController::_update_file_auths($group);
			$auths = F('Auths/'.$group);
		}
		return $auths;
	}
	
	/**
	 * 更新权限的文件缓存
	 * @param unknown $group   admin,home
	 */
	public function _update_file_auths($group){
		$rmap['group'] = array('like',$group.'%');
		$rules = M('AuthRule')->where($rmap)->order('listorder asc')->select();
		$data = array();
		foreach($rules as $r){
			$tmp = explode('.', $r['name']);
			$tmp = explode('/', $tmp[0]);
			$_group = $tmp[1];
			$_module = $tmp[2];
			$tmp[3] ? $_action = $tmp[3] : $_action = 'index';
	
			$data[$_group][$_module][$_action] = $r['id']; //权限ID对应
		}
		F('Auths/'.$group,$data);
	}
	
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('id' => array('in', $id));
		$info = $this->m->where($map)->find();
		if($this->m->where($map)->delete()){
			$this->_update_file_auths($info['group']); //更新到文件缓存
			$this->_update_cache_tree($info['group']);
			$this->success('删除成功',__URL__);
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function listorders() {
		$status = parent::_listorders($this->m);
		if ($status) {
			$this->success("排序更新成功！",U('index'));
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	/**
	 * 固化app分类树存储
	 * @param unknown $appid
	 * @param unknown $groupid
	 */
	public function _update_cache_tree($group){
		$tree = new \Think\Tree();
	
		$map['group'] = $group;
		$data = $this->m->where($map)->order('listorder desc')->select();
		
		$tree->init($data);
		$treedata = $tree->get_tree_array(0, $str);
	
		F('AuthsTree/'.$group,$treedata);
		return $treedata;
	}
	
	/**
	 * ajax 获取tree插件节点数据
	 */
	public function ajax_nodes(){
		
		$tree = new \Think\Tree();
		$treedata = F('AuthsTree/'.$_SESSION['auth_group']);
		if(!$treedata){
			$treedata = $this->_update_cache_tree($_SESSION['auth_group']);
		}

		
	
		$results = array_filter(explode('|', $this->_get_node_tpl($treedata, $result)));
		$last = array();
		foreach($results as $r){
			$last[] = json_decode($r,true);
		}
		//echo json_encode($last);

		$this->ajaxReturn($last);
	}
	
	/**
	 * 配合ajax_nodes
	 * @param unknown $datas
	 * @param unknown $result
	 * @return string
	 */
	public function _get_node_tpl($datas,$result){
		$results = '';
		foreach ($datas as $data){
			$res= array(
					'id' => $data['id'],
					'pId' => $data['parentid'],
					'name' => $data['title'],
					'open' => true
			);
			$results .= '|'.json_encode($res);
			if($data['child']){
				$results .= '|'.$this->_get_node_tpl($data['child'], $result);
			}
		}
		return $results;
	}
}
?>